<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>发送邮件</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: Arial, sans-serif;
            height: 100vh;
            background-color: #f7f7f7;
            display: flex;
            justify-content: center;
            align-items: center;
        }
        .container {
            width: 80%;
            max-width: 800px;
            background-color: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        .toolbar {
            display: flex;
            justify-content: space-between;
            margin-bottom: 20px;
        }
        .toolbar button {
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
        }
        .toolbar button:hover {
            background-color: #0056b3;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        .form-group input, .form-group textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 16px;
        }
        .form-group textarea {
            height: 150px;
            resize: vertical;
        }
        .ai-modal {
            display: none;
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: white;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            border-radius: 10px;
            z-index: 1000;
            width: 500px;
        }
        .ai-modal h2 {
            margin-bottom: 20px;
        }
        .ai-modal textarea {
            width: 100%;
            height: 100px;
            margin-bottom: 10px;
            resize: none;
        }
        .ai-modal button {
            padding: 10px 15px;
            margin-right: 10px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
        }
        .ai-modal button:hover {
            background-color: #0056b3;
        }
        .success-popup, .error-popup {
            display: none;
            position: fixed;
            top: 20%;
            left: 50%;
            transform: translate(-50%, -50%);
            background-color: #4caf50;
            color: white;
            padding: 20px;
            border-radius: 10px;
            text-align: center;
            z-index: 1000;
        }
        .success-popup button, .error-popup button {
            background-color: white;
            color: #4caf50;
            border: none;
            padding: 10px 20px;
            font-size: 16px;
            margin-top: 10px;
            cursor: pointer;
            border-radius: 5px;
        }
        .error-popup {
            background-color: #f44336;
        }
        .error-popup button {
            color: #f44336;
        }
    </style>
    <script>
        function sendEmail(event) { 
            event.preventDefault(); 
            const form = event.target; 
            const formData = new FormData(form); 
            fetch(form.action, { 
                method: form.method, 
                body: formData 
            }).then(response => response.json()) 
            .then(data => { 
                if (data.status === 'success') { 
                    document.getElementById('success-popup').style.display = 'block'; 
                } else { 
                    document.getElementById('error-popup').style.display = 'block'; 
                    document.getElementById('error-message').innerText = data.message; 
                } 
            }).catch(error => { 
                document.getElementById('error-popup').style.display = 'block'; 
                document.getElementById('error-message').innerText = error.message; 
            });
        } 
        function closeSuccessPopup() { 
            document.getElementById('success-popup').style.display = 'none'; 
        } 
        function closeErrorPopup() { 
            document.getElementById('error-popup').style.display = 'none';
        }
        // AI写信模态框逻辑
        function toggleAiModal() {
            const modal = document.getElementById('aiModal');
            modal.style.display = modal.style.display === 'block' ? 'none' : 'block';
        }
        // 生成AI内容逻辑
        function generateAiContent() {
            const input = document.getElementById('aiInput').value;
            fetch('/ai_generate_email', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ prompt: input })
            })
                .then(response => response.json())
                .then(data => {
                    document.getElementById('aiOutput').innerText = data.emailContent || '生成失败，请重试。';
                })
                .catch(() => {
                    document.getElementById('aiOutput').innerText = '请求错误，请稍后再试。';
                });
        }
    </script>
</head>
<body>
    <div class="container">
        <div class="toolbar">
            <button onclick="document.getElementById('sendButton').click();">发送</button>
        </div>
        <form action="{{ url_for('main.send_email') }}" method="POST" onsubmit="sendEmail(event)">
            <div class="form-group">
                <label for="receiver">收件人：</label>
                <input type="email" id="receiver" name="receiver" placeholder="收件人邮箱" required>
            </div>
            <div class="form-group">
                <label for="subject">主题：</label>
                <input type="text" id="subject" name="subject" placeholder="邮件主题" required>
            </div>
            <div class="form-group">
                <label for="body">正文：</label>
                <textarea id="body" name="body" placeholder="请输入邮件内容" required></textarea>
            </div>
            <button type="submit" id="sendButton" style="display: none;">隐藏发送</button>
        </form>
        <button onclick="toggleAiModal()">AI辅助写信</button>
        <!-- AI辅助模态框 -->
        <div id="aiModal" class="ai-modal">
            <h2>AI辅助写信</h2>
            <textarea id="aiInput" placeholder="请输入提示内容"></textarea>
            <button onclick="generateAiContent()">生成</button>
            <button onclick="toggleAiModal()">关闭</button>
            <p id="aiOutput"></p>
        </div>
        <!-- 成功弹窗 -->
        <div id="success-popup" class="success-popup">
            <p>邮件发送成功！</p>
            <button onclick="closeSuccessPopup()">关闭</button>
        </div>
        <!-- 错误弹窗 --> 
        <div id="error-popup" class="error-popup"> 
            <p>邮件发送失败：<span id="error-message"></span></p> 
            <button onclick="closeErrorPopup()">关闭</button> 
        </div>
    </div>
</body>
</html>
